//
//  JQFYTicketPageViewController.m
//  JQFY
//
//  Created by apple on 2017/8/28.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYTicketPageViewController.h"
#import "JQFYNavigationTabBar.h"
#import "JQFYActivityViewController.h"
#import "JQFYGroupListViewController.h"
#import "JQFYRhythmView.h"
#import "JQFYPlayViewController.h"
#import "JQFYChooseCityViewController.h"
#import "JQFYMusicPlayer.h"

#define ScreenH [UIScreen mainScreen].bounds.size.height
#define ScreenW [UIScreen mainScreen].bounds.size.width



@interface JQFYTicketPageViewController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource>

@property(nonatomic,strong)JQFYNavigationTabBar *navigationTabBar;
@property(nonatomic,strong)NSArray<UIViewController *> *subViewControllers;
@property(nonatomic, strong)JQFYRhythmView *rhythmView;
@property (nonatomic,strong) ZJAnimationPopView *alert;
@end

@implementation JQFYTicketPageViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    if (__Player__.isPlaying) {
        
        if (self.rhythmView.animation) {
            return;
        }
        [self.rhythmView beginAnimation];
    }
    if (__Player__.isPlaying == NO) {
        
        [self.rhythmView stopAnimation];
    }
}

-(JQFYNavigationTabBar *)navigationTabBar
{
    if (!_navigationTabBar) {
        self.navigationTabBar = [[JQFYNavigationTabBar alloc] initWithTitles:@[@"活动",@"群聊"]];
        __weak typeof(self) weakSelf = self;
        self.navigationTabBar.frame = CGRectMake(50, 0, 100, 44);
        [self.navigationTabBar setDidClickAtIndex:^(NSInteger index){
            [weakSelf navigationDidSelectedControllerIndex:index];
        }];
    }
    return _navigationTabBar;
}

-(NSArray *)subViewControllers
{
    if (!_subViewControllers) {
        JQFYActivityViewController *controllerOne = [[JQFYActivityViewController alloc] init];
        JQFYGroupListViewController *controllerTwo = [[JQFYGroupListViewController alloc] init];
        self.subViewControllers = @[controllerOne,controllerTwo];
    }
    return _subViewControllers;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    UIView *titleBgView = [[UIView alloc] init];
//    titleBgView.frame = CGRectMake(0, 0, 100, 40);
//    titleBgView.backgroundColor = [UIColor clearColor];
//    self.navigationItem.titleView = titleBgView;
//    [titleBgView addSubview:self.navigationTabBar];
//    [_navigationTabBar mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(titleBgView);
//    }];
    self.navigationItem.titleView = self.navigationTabBar;
    //设置右边音乐动图
    self.rhythmView = [[JQFYRhythmView alloc]initWithFrame:CGRectMake(DH_Screen_Width - 40 *SCAL, 12 *SCAL, 20 *SCAL, 20 *SCAL)];
    [self.navigationController.navigationBar addSubview:self.rhythmView];
    //添加手势
    UITapGestureRecognizer *rhythmGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRhythm)];
    [self.rhythmView addGestureRecognizer:rhythmGesture];

    self.delegate = self;
    self.dataSource = self;
    [self setViewControllers:@[self.subViewControllers.firstObject]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:NO
                  completion:nil];
    [self setValue:@"UIPageViewControllerTransitionStyleScroll" forKey:NSStringFromSelector(@selector(transitionStyle))];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rhythmViewAnimation:) name:JQFYMusicPlayerStatusDidChangedNotification object:nil];
}

#pragma mark - 律动条动画 -
- (void)rhythmViewAnimation:(NSNotification *)noti
{
    JQFYMusicPlayerStatus status = [noti.userInfo[kMusicPlayerStatus] integerValue];
    
    if (status == JQFYMusicPlayerStatusPlaying) {
        if (self.rhythmView.animation) {
            return;
        }
        [self.rhythmView beginAnimation];
    } else {
        [self.rhythmView stopAnimation];
    }
}

#pragma mark - UIPageViewControllerDelegate
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSInteger index = [self.subViewControllers indexOfObject:viewController];
    if(index == 0 || index == NSNotFound) {
        return nil;
    }
    
    return [self.subViewControllers objectAtIndex:index - 1];
}

- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSInteger index = [self.subViewControllers indexOfObject:viewController];
    if(index == NSNotFound || index == self.subViewControllers.count - 1) {
        return nil;
    }
    return [self.subViewControllers objectAtIndex:index + 1];
}

- (void)pageViewController:(UIPageViewController *)pageViewController
        didFinishAnimating:(BOOL)finished
   previousViewControllers:(NSArray *)previousViewControllers
       transitionCompleted:(BOOL)completed {
    UIViewController *viewController = self.viewControllers[0];
    NSUInteger index = [self.subViewControllers indexOfObject:viewController];
    [self.navigationTabBar scrollToIndex:index];
}

#pragma mark - PrivateMethod
- (void)navigationDidSelectedControllerIndex:(NSInteger)index {
    [self setViewControllers:@[[self.subViewControllers objectAtIndex:index]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
#pragma mark - 跳转播放器界面 -
-(void)tapRhythm{
    NSLog(@"点击了播放器界面");
    if (__Player__.playingSong == nil) {
        UIImageView *alertError = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"前往音乐（合并）"]];
        alertError.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [alertError addGestureRecognizer:tap];
        self.alert = [[ZJAnimationPopView alloc] initWithCustomView:alertError popStyle:ZJAnimationPopStyleScale dismissStyle:ZJAnimationDismissStyleScale];
        self.alert.popBGAlpha = 0.6;
        self.alert.isClickBGDismiss = YES;
        [self.alert pop];
        return;
    }
    //播放界面
    JQFYPlayViewController *playerVc = [[JQFYPlayViewController alloc] init];
    [self presentViewController:playerVc animated:YES completion:nil];
}
- (void)tap{
    [self.alert dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
